<template>
  <el-dialog :visible.sync="dialogVisible" width="600px"  :close-on-click-modal="false" :close-on-press-escape	='false'
             title="绑定LinkedIn账号" center>
    <el-form :model="form" ref="ruleForm" :rules="rules" @keyup.enter.native='submit'  class="social-bind">
      <el-form-item label="LinkedIn账号" prop="accountEmail">
        <el-input v-model="form.accountEmail" size="medium"  placeholder="请输入LinkedIn账号" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密 码" prop="password" class="password">
        <el-input v-model="form.password" size="medium" :type="!state?'password':'text'"  placeholder="请输入账号密码" autocomplete="off"></el-input>
        <i :class="state?'el-icon-view':'icon-yanjing'"
           :style="{top:state?'66%':'52%'}"
           @click="state=!state"></i>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" align="right">
      <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" size="mini" @click="submit" :loading="loading">{{loading?'绑定中':'确 定'}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {getAuthURLLin} from '@/api/automation/socialAccount/bindLinkedin'
    import {getToken} from '@/util/auth'
    import {setCookie} from '@/util/util'
    export default {
        name: "DialogFacebook",
        data() {
            return {
                dialogVisible: false,
                form: {
                    accountEmail: '',
                    password: ''
                },
                rules: {
                    accountEmail: [
                        {required: true, message: '请输入LinkedIn账号', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入账号密码', trigger: 'blur'},
                    ]
                },
                state:false,
                loading:false
            }
        },
        methods: {
            submit() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        //提交
                        this.loading=true;
                        this.form.accountEmail =encodeURIComponent(this.form.accountEmail);
                        getAuthURLLin(this.form).then(res => {
                            this.loading=false
                            if(res.data.success){
                                const result = res.data.data;
                                if(result.authUrl!=''){
                                    setCookie('mYToken',getToken());
                                    setCookie('mYAccount',this.form.accountEmail);
                                    this.dialogVisible = false;
                                    window.open(result.authUrl,'','toolsbar=no,menubar=no,resizable=yes,scrollbars=yes,width=1000,height=700')
                                }else{
                                    this.$message({
                                        message: '绑定授权地址获取失败',
                                        type: 'success'
                                    });
                                }
                            }else{
                                this.form.accountEmail =decodeURIComponent(this.form.accountEmail);
                            }
                        }).catch(()=>{this.loading=false})
                    } else {
                        return false;
                    }
                });
            },
            init() {
                if (this.$refs.ruleForm) {
                    this.$refs.ruleForm.resetFields();
                }
                this.dialogVisible = true;
            }
        }
    }
</script>

<style scoped>

</style>
